//index.js
//获取应用实例
var user_js = require('../../utils/user.js');
var index_js = require('../../utils/index.js');

var app = getApp();
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    menu: false,
    my_index: [],
    start_index: 0,
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this;
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  },
  onShow:function(){
    var that = this;
    that.refresh_index_list();
  },
  refresh_index_list: function () {
    var that = this;
    var start_index = that.data.start_index
    console.log('on show.');
    index_js.my_index(start_index, function (res) {
      console.log(res);
      if (res.data.op == 'my_index') {
        var my_index = that.data.my_index;
        var new_index = res.data.data;
        for (var i in new_index ) {
          my_index.push(new_index[i]);
        }
        that.setData({
            my_index: my_index
        });
        var new_index = that.data.my_index.length;
        that.setData({
          start_index: new_index
        });
      }
    });
  },

  goto_search: function () {
    wx.navigateTo({
      url: '../search/search'
    })
  }, 
  create_single: function () {
    wx.navigateTo({
      url: '../create/index'
    })
  }, 
  create_schedule: function () {
    wx.navigateTo({
      url: '../classSchedule/index'
    })
  }, 
  activity_history: function () {
    wx.navigateTo({
      url: '../mine/recording/index'
    })
  }, 
  help: function () {
    wx.navigateTo({
      url: '../mine/help/index'
    })
  }, 
  go_to_view: function (e) {
    return false;
    var calendar_id = e.target.dataset.calendar_id;
    var activity_id = e.target.dataset.activity_id;
    console.log(calendar_id);
    console.log(activity_id);
    if (calendar_id != 0 && activity_id == 0) {
      wx.navigateTo({
        url: '../create/calendar?id=' + calendar_id,
      })
    }else {
      wx.navigateTo({
        url: '../activity/detail?id=' + activity_id,
      })
    }
  },
  onReachBottom:function (e) {
    console.log(123);
    var that = this;
    that.refresh_index_list();
  },

})
